package com.css.dzj.dzjfxyj.modules.acquier.Controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjfxyj.common.util.R;
import com.css.dzj.dzjfxyj.modules.acquier.entity.JgFxyjWarningSignalEntity;
import com.css.dzj.dzjfxyj.modules.acquier.service.AcquierService;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping(value = "/acquier")
public class AcquierController {
    @Autowired
    private AcquierService acquierService;
    @Autowired
    BaseService baseService;
    /**
     *分页查询所有(认领)
     */
    @GetMapping(value = "/getAllAcquier")
    public R getAllAcquier(@RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize,
                         @RequestParam(name = "fxyjDomainId", required = false) String fxyjDomainId,
                         @RequestParam(name = "fxyjTitle", required = false) String fxyjTitle,
                         @RequestParam(name = "fxyjLevel", required = false) String fxyjLevel,
                         @RequestParam(name = "fxyjObjectName", required = false) String fxyjObjectName,
                         @RequestParam(name = "startTime1", required = false) String startTime1,
                         @RequestParam(name = "endTime1", required = false) String endTime1,
                         @RequestParam(name = "startTime", required = false) String startTime,
                         @RequestParam(name = "endTime", required = false) String endTime) {
        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        QueryResults results = acquierService.selectAll(fxyjDomainId,fxyjTitle,fxyjLevel,fxyjObjectName,startTime1,endTime1,startTime,endTime,pageRequest);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", results);
    }

    /**
     *分页查询所有(综合)
     */
    @GetMapping(value = "/getAllAcquierzh")
    public R getAllAcquierzh(@RequestParam(required = false) Integer pageNo,
                           @RequestParam(required = false) Integer pageSize,
                           @RequestParam(name = "fxyjDomainId", required = false) String fxyjDomainId,
                           @RequestParam(name = "fxyjTitle", required = false) String fxyjTitle,
                           @RequestParam(name = "fxyjLevel", required = false) String fxyjLevel,
                           @RequestParam(name = "fxyjObjectName", required = false) String fxyjObjectName,
                           @RequestParam(name = "dealState", required = false) String dealState,
                           @RequestParam(name = "startTime", required = false) String startTime,
                           @RequestParam(name = "endTime", required = false) String endTime) {
        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        QueryResults results = acquierService.selectAllzh(fxyjDomainId,fxyjTitle,fxyjLevel,fxyjObjectName,dealState,startTime,endTime,pageRequest);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", results);
    }

    /**
     * 根据id查询详细信息
     * @return
     */
    @PostMapping("/findById")
    public R findById(String id){
        if (StringUtils.isEmpty(id)){
            baseService.logFailed(3,"查询失败","2");
            return null;
        }else {
            JgFxyjWarningSignalEntity jgFxyjWarningSignalEntity = acquierService.findById(id);
            baseService.logSuccess(3,"查询失败","2");
            return R.ok().put("data",jgFxyjWarningSignalEntity);
        }
    }

    /**
     * 保存
     * @param jgFxyjWarningSignalEntity
     * @param request
     * @return
     */
    @PostMapping("/saveAcquire")
    public R saveAcquire(JgFxyjWarningSignalEntity jgFxyjWarningSignalEntity, HttpServletRequest request){
        try {
            R r = acquierService.saveAcquire(jgFxyjWarningSignalEntity,request);
            if (r.getCode()=="500"){
                baseService.logFailed(3,"保存失败","2");
                return R.error(r.getMsg());
            }else {
                baseService.logFailed(3,"保存成功","2");
                return R.ok();
            }
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"保存失败","2");
            return R.error(e.getMessage());
        }

    }

    /**
     * 认领操作修改
     * @param jgFxyjWarningSignalEntity
     * @param request
     * @return
     */
    @PostMapping("/updAcquire")
    public R updAcquire(@RequestBody JgFxyjWarningSignalEntity jgFxyjWarningSignalEntity, HttpServletRequest request){
        try {
            acquierService.updAcquire(jgFxyjWarningSignalEntity,request);
            baseService.logSuccess(3,"认领操作修改成功","2");
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"认领操作修改失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改状态
     * @param id
     * @return
     */
    @PostMapping("/updState")
    public R updState(String id){
        try {
            R r = acquierService.updState(id);
            if (r.getCode()=="500"){
                baseService.logFailed(3,"修改失败","2");
                return R.error(r.getMsg());
            }else {
                baseService.logSuccess(3,"修改成功","2");
                return R.ok();
            }
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }
    @GetMapping("/account")
    public Long getCount(){
       Long count=acquierService.getCount();
        return count;
    }
}
